package com.chenjiahao.kaiyanwan.ui.discovery

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.chenjiahao.kaiyanwan.BUNDLE_VIDEO_DATA
import com.chenjiahao.kaiyanwan.R
import com.chenjiahao.kaiyanwan.base.CommonAdapter
import com.chenjiahao.kaiyanwan.base.ViewHolder
import com.chenjiahao.kaiyanwan.durationFormat
import com.chenjiahao.kaiyanwan.startActivity
import com.chenjiahao.kaiyanwan.ui.videodetail.VideoDetailActivity
import com.hazz.kotlinmvp.mvp.model.bean.HomeBean

/**
 * Created by chenjh on 2020/9/17
 */
class FollowHorizontalAdapter (mContext: Context, categoryList: ArrayList<HomeBean.Issue.Item>, layoutId: Int) :
    CommonAdapter<HomeBean.Issue.Item>(mContext, categoryList, layoutId) {

    /**
     * 绑定数据
     */
    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        val horizontalItemData = data.data
        holder.setImagePath(R.id.iv_cover_feed, object : ViewHolder.HolderImageLoader(data.data?.cover?.feed!!) {
            override fun loadImage(iv: ImageView, path: String) {
                // 加载封页图
                Glide.with(mContext)
                    .load(path)
                    .placeholder(R.drawable.placeholder_banner)
                    .transition(DrawableTransitionOptions().crossFade())
                    .into(holder.getView(R.id.iv_cover_feed))
            }

        })

        //横向 RecyclerView 封页图下面标题
        holder.setText(R.id.tv_title, horizontalItemData?.title ?: "")

        // 格式化时间
        val timeFormat = durationFormat(horizontalItemData?.duration)
        //标签
        with(holder) {
            if (horizontalItemData?.tags != null && horizontalItemData.tags.size > 0) {
                setText(R.id.tv_tag, "#${horizontalItemData.tags[0].name} / $timeFormat")
            }else{
                setText(R.id.tv_tag,"#$timeFormat")
            }

            setOnItemClickListener(listener =View.OnClickListener {
                startActivity<VideoDetailActivity>(mContext as Activity, holder.getView(R.id.iv_cover_feed)){
                    putExtra(BUNDLE_VIDEO_DATA, data)
                    putExtra(VideoDetailActivity.Companion.TRANSITION, true)
                }
            })
        }

    }


}
